# portion library

BASE=/opt

# folder where deb packages are deployed
# (this is only relevant if you are hosting a package repository)
PACKAGE_FOLDER=/mnt/web/www/ubuntu

# returns the 'current' path for the specified package
# param: {package-name}
get_current_path() {
	echo "${BASE}/${1}/current"
}

# returns the versioned package path 
# param: {package-name} {package-version}
get_version_path() {
	echo "${BASE}/${1}/${1}_${2}"
}

# symlink package to current
add_package_symlink() {

    local current_path
    current_path=$(get_current_path ${1})

    local version_path
    version_path=$(get_version_path ${1} ${2})

    ln -s ${version_path} ${current_path}
}

# deletes the current symlink
delete_package_symlink() {
    local current_path
    current_path=$(get_current_path ${1})

    rm ${current_path}
}

# add path entry for package
# param: path for package e.g. /opt/foo/current/bin
add_path_entry() {

    if [ -z "$(grep ${1} /etc/portion.path)" ]; then
    
        printf '\nPATH=%s:$PATH' ${1} >> /etc/portion.path
    fi

}

# Add an environment entry
# param: key value e.g. JAVA_HOME /opt/java/current
add_env_entry() {

    if [ -z "$(grep ${1} /etc/portion.env)" ]; then
        
        printf '\nexport %s=%s' ${1} ${2} >> /etc/portion.env
    fi
}

# make blackbox debian package
bb_make_package() {
    
    rm -rf target
    fakeroot epm -a amd64 -f deb --output-dir target $(basename $(pwd)) manifest
}

# deploy specified deb package to repository (ONLY FOR REPO SERVER -- WILL NOT RUN ON CLIENT)
bb_deploy_package() {

    reprepro -b ${PACKAGE_FOLDER} -V -P 1 -S main includedeb fimero ${1}
}
